Imports System.IO


Public Class frmEndUserForm
    Private m_upload As Boolean
    Private m_speed As BK_SPEEDS = My.Settings.endSpeed
    Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If m_upload = False Then
            Dim f_code(15) As Byte
            Dim rand As New Random()
            Dim f_BLoffset As Integer = 3952
            Dim f_EEoffset As Integer = 136
            Dim f_EEprom(143) As Byte
            Dim f_firmware() As Byte = Nothing
            Dim f_bootloader() As Byte = Nothing

            Select Case m_speed
                Case BK_SPEEDS.BK_8MHz
                    f_bootloader = CryptResource(getResource("Boot_8.enc"), False).ToArray
                    f_firmware = CryptResource(getResource("BIKI_OS_1.0.8.enc"), False).ToArray
                Case BK_SPEEDS.BK_16Mhz
                    f_bootloader = CryptResource(getResource("Boot_16.enc"), False).ToArray
                    f_firmware = CryptResource(getResource("BIKI_OS_1.0.16.enc"), False).ToArray
                Case BK_SPEEDS.BK_12MHz
                    f_bootloader = CryptResource(getResource("Boot_12.enc"), False).ToArray
                    f_firmware = CryptResource(getResource("BIKI_OS_1.0.12.enc"), False).ToArray
            End Select

            If f_bootloader.Length = 0 Or _
              f_firmware.Length = 0 _
           Then
                MsgBox("Error loading resources")
                Exit Sub
            End If

            For i As Integer = 0 To 12
                f_EEprom(i) = 255
            Next
            Dim f_count As Integer = 0
            For i As Integer = 1 To 8
                rand.NextBytes(f_code)
                f_count = 0
                For Each bit As Byte In f_code
                    f_EEprom(f_EEoffset - (f_count * 8)) = bit
                    f_count += 1
                Next
                f_BLoffset += 16
                f_EEoffset += 1
            Next
            Dim s_num(2) As Byte
            rand.NextBytes(s_num)
            f_EEprom(12) = s_num(0)
            f_EEprom(13) = s_num(1)
            f_EEprom(14) = s_num(2)

            Dim emerg As New Program(m_speed, True)
            emerg.flash = f_bootloader
            emerg.eeprom = f_EEprom
            emerg.firmware = f_firmware

            bwg.WorkerReportsProgress = True
            bwg.WorkerSupportsCancellation = True
            bwg.RunWorkerAsync(emerg)
            m_upload = True

        End If
    End Sub

    'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Dim rand As New Random()
    '    Dim f_firm() As Byte

    '    Dim f_inStream As New MemoryStream(arrayLoad(Application.StartupPath & "\data1.dat"))
    '    Dim f_outStream As New MemoryStream
    '    CryptFile("This IS! not a TEST moFO", f_inStream, f_outStream, False)
    '    'arrayDump(f_outStream.ToArray, "c:\firmware.dec")
    '    f_firm = f_outStream.ToArray
    '    f_inStream.Close()
    '    f_outStream.Close()

    '    Dim f_encIn As New MemoryStream(arrayLoad(Application.StartupPath & "\data2.dat"))
    '    Dim f_decOut As New MemoryStream
    '    CryptFile("This IS! not a TEST moFO", f_encIn, f_decOut, False)

    '    Dim f_bootloader(4095) As Byte
    '    Dim f_bootdec() As Byte
    '    Dim f_code(15) As Byte

    '    f_bootdec = f_decOut.ToArray
    '    array_copy(f_bootdec, 8192, f_bootloader, 0, 4096)
    '    f_encIn.Close()
    '    f_decOut.Close()


    '    Dim f_BLoffset As Integer = 3952
    '    Dim f_EEoffset As Integer = 136
    '    Dim f_EEprom(143) As Byte
    '    For i As Integer = 0 To 12
    '        f_EEprom(i) = 255
    '    Next
    '    Dim f_count As Integer = 0
    '    For i As Integer = 1 To 8
    '        rand.NextBytes(f_code)
    '        f_count = 0
    '        For Each bit As Byte In f_code
    '            f_bootloader(f_BLoffset + f_count) = bit
    '            f_EEprom(f_EEoffset - (f_count * 8)) = bit
    '            f_count += 1
    '        Next
    '        f_BLoffset += 16
    '        f_EEoffset += 1
    '    Next

    '    Dim s_num(1) As Byte
    '    rand.NextBytes(s_num)
    '    f_EEprom(13) = s_num(0)
    '    f_EEprom(14) = s_num(1)
    '    f_EEprom(15) = 85

    '    SPI_Connect()
    '    spi_reset()

    '    Threading.Thread.Sleep(10)
    '    SPI_out(172)
    '    SPI_out(160)
    '    SPI_in()
    '    SPI_out(63)
    '    Threading.Thread.Sleep(10)
    '    SPI_out(172)
    '    SPI_out(168)
    '    SPI_in()
    '    SPI_out(216)
    '    Threading.Thread.Sleep(10)
    '    SPI_out(172)
    '    SPI_out(164)
    '    SPI_in()
    '    SPI_out(127)
    '    Threading.Thread.Sleep(10)
    '    SPI_out(172)
    '    SPI_out(255)
    '    SPI_in()
    '    SPI_out(205)
    '    SPI_disconnect()


    '    setProgram(f_bootloader, False, True)
    '    setEEprom(f_EEprom)

    '    SPI_Connect()
    '    spi_reset()
    '    SPI_out(172)
    '    SPI_out(255)
    '    SPI_in()
    '    SPI_out(204)
    '    Threading.Thread.Sleep(10)
    '    SPI_disconnect()
    '    Dim inp As String
    '    If BikiRom_BootloaderUpload(f_firm) Then
    '        Err_message("Board Programming complete")
    '        inp = InputBox("Enter the serial Number written on the back of your BikiRom USB Megaboard", "Serial Number")
    '    Else
    '        Err_message("Board Programming Failed.. try again")
    '        Exit Sub
    '    End If


    '    'arrayDump(f_bootloader, "c:\bl-dump.bin")
    '    Dim f_eeDump As New MemoryStream(f_EEprom)
    '    Dim f_eeDump2 As New MemoryStream

    '    CryptFile("This IS! not a TEST moFO But This one IS", f_eeDump, f_eeDump2, True)
    '    arrayDump(f_eeDump2.ToArray, Application.StartupPath & "\" & inp & ".bmd")
    '    MsgBox("Inside the directory you extracted this program is now a file named '" & inp & ".bmd, this needs to be emailed back to me " & _
    '    "at badbiki@ii.net Subject:Board Update return.. cheers", MsgBoxStyle.Information, "Infomation")

    'End Sub
    Public Sub arrayDump(ByRef sArray() As Byte, ByVal sOutFile As String)
        Dim fStream As New FileStream(sOutFile, FileMode.Create)
        'Dim bWrite As New (fStream)
        fStream.Write(sArray, 0, sArray.Length)
        fStream.Close()
    End Sub

    Private Sub bwg_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwg.DoWork
        'This method will run on a thread other than the UI thread.
        'Be sure not to manipulate any Windows Forms controls created
        'on the UI thread from this method.
        Dim em As Program = CType(e.Argument, Program)
        AddHandler em.Message, AddressOf EventProgress
        AddHandler em.Progress, AddressOf EventProgress

        em.Execute(bwg, e)
    End Sub
    Private Sub EventProgress( _
       ByVal sender As Object, ByVal e As Emergency_Args)

        ' A file was found. Report the progress, triggering the
        ' BackgroundWorker.ProgressChanged event:
        Dim intPercent As Integer = e.Progress
        If intPercent >= 100 Then
            intPercent = 100
        ElseIf intPercent < 0 Then
            intPercent = 0
        End If
        bwg.ReportProgress(intPercent, e)
    End Sub
    Private Sub bwUpload_ProgressChanges( _
        ByVal sender As Object, _
        ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
        Handles bwg.ProgressChanged

        Dim state As Emergency_Args = CType(e.UserState, Emergency_Args)
        setBar(state.Progress)
        Err_message(state.Message)
    End Sub
    Private Sub bwUpload_RunWorkerCompleted( _
        ByVal sender As Object, _
        ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
        Handles bwg.RunWorkerCompleted
        m_upload = False
    End Sub

End Class
